unit frm_FilterToDo;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, shbl_Filter, PropFilerEh, PropStorageEh, ActnList, Menus,
  StdCtrls, Buttons, ExtCtrls, DBGridEh, DBCtrlsEh, FIBDatabase,
  pFIBDatabase, DB, FIBDataSet, pFIBDataSet, DBLookupEh, Mask, ComCtrls;

type
  TfrmFilterToDo = class(TshblFilter)
    dsTraits: TpFIBDataSet;
    dsTraitsID: TFIBIntegerField;
    dsTraitsNAME: TFIBStringField;
    trLists: TpFIBTransaction;
    dsMakers: TpFIBDataSet;
    dscMakers: TDataSource;
    dsStreets: TpFIBDataSet;
    dscStreets: TDataSource;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    cbContragent10: TDBCheckBoxEh;
    eContragent: TDBLookupComboboxEh;
    cbClosed12: TDBCheckBoxEh;
    cbAlarm: TDBCheckBoxEh;
    pAlarm: TPanel;
    rbAlarm: TRadioButton;
    RadioButton4: TRadioButton;
    cbStatus: TDBCheckBoxEh;
    pStatus: TPanel;
    rbStatus: TRadioButton;
    RadioButton2: TRadioButton;
    cbMaker: TDBCheckBoxEh;
    eMaker: TDBLookupComboboxEh;
    dMake: TDBCheckBoxEh;
    Label3: TLabel;
    dteMakeBdate: TDBDateTimeEditEh;
    Label4: TLabel;
    dteMakeEdate: TDBDateTimeEditEh;
    cbNote: TDBCheckBoxEh;
    eNote: TDBEditEh;
    cbIsDeleted13: TDBCheckBoxEh;
    cbTraits14: TDBCheckBoxEh;
    pnlTraits: TPanel;
    SrcLabel: TLabel;
    DstLabel: TLabel;
    IncludeBtn: TSpeedButton;
    IncAllBtn: TSpeedButton;
    ExcludeBtn: TSpeedButton;
    ExAllBtn: TSpeedButton;
    Panel3: TPanel;
    SrcList: TListBox;
    Panel4: TPanel;
    DstList: TListBox;
    Label2: TLabel;
    dteEdate: TDBDateTimeEditEh;
    Label1: TLabel;
    cbStreet2: TDBCheckBoxEh;
    cbHouse3: TDBCheckBoxEh;
    cbApparts4: TDBCheckBoxEh;
    eHouse: TDBEditEh;
    eApparts: TDBEditEh;
    cbToDoDate2: TDBCheckBoxEh;
    dteBdate: TDBDateTimeEditEh;
    cbWorker: TDBCheckBoxEh;
    eWorker: TDBLookupComboboxEh;
    lcbWorkName: TDBLookupComboboxEh;
    cbWorkName: TDBCheckBoxEh;
    eStreet: TDBLookupComboboxEh;
    cbName1: TDBCheckBoxEh;
    eName: TDBEditEh;
    Panel2: TPanel;
    rbClosed: TRadioButton;
    RadioButton3: TRadioButton;
    pDeleted: TPanel;
    cbDeleted: TRadioButton;
    RadioButton1: TRadioButton;
    procedure cbName1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure aFilterClearExecute(Sender: TObject);
    procedure IncludeBtnClick(Sender: TObject);
    procedure IncAllBtnClick(Sender: TObject);
    procedure ExcludeBtnClick(Sender: TObject);
    procedure ExAllBtnClick(Sender: TObject);
    procedure aApplyExecute(Sender: TObject);
    procedure dteBdateChange(Sender: TObject);
    procedure dteMakeBdateChange(Sender: TObject);
    procedure eStreetKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    procedure FillListBox;
    procedure MoveSelected(List: TCustomListBox; Items: TStrings);
    procedure SetItem(List: TListBox; Index: Integer);
    function GetFirstSelection(List: TCustomListBox): Integer;
    procedure SetButtons;

  end;

var
  frmFilterToDo: TfrmFilterToDo;

implementation

uses MAIN, rst_ToDo, MAIN_DM;

{$R *.dfm}

procedure TfrmFilterToDo.FillListBox;
begin
  if not dsTraits.Active then exit;
  dsTraits.First;
  while not dsTraits.Eof do
  begin
    SrcList.AddItem(dsTraitsNAME.AsString, TObject(dsTraitsID.AsInteger));
    dsTraits.Next;
  end;
end;

procedure TfrmFilterToDo.IncludeBtnClick(Sender: TObject);
var
  Index: Integer;
begin
  Index := GetFirstSelection(SrcList);
  MoveSelected(SrcList, DstList.Items);
  SetItem(SrcList, Index);
end;

procedure TfrmFilterToDo.ExcludeBtnClick(Sender: TObject);
var
  Index: Integer;
begin
  Index := GetFirstSelection(DstList);
  MoveSelected(DstList, SrcList.Items);
  SetItem(DstList, Index);
end;

procedure TfrmFilterToDo.IncAllBtnClick(Sender: TObject);
var
  I: Integer;
begin
  for I := 0 to SrcList.Items.Count - 1 do
    DstList.Items.AddObject(SrcList.Items[I],
      SrcList.Items.Objects[I]);
  SrcList.Items.Clear;
  SetItem(SrcList, 0);
end;

procedure TfrmFilterToDo.ExAllBtnClick(Sender: TObject);
var
  I: Integer;
begin
  for I := 0 to DstList.Items.Count - 1 do
    SrcList.Items.AddObject(DstList.Items[I], DstList.Items.Objects[I]);
  DstList.Items.Clear;
  SetItem(DstList, 0);
end;

procedure TfrmFilterToDo.MoveSelected(List: TCustomListBox; Items: TStrings);
var
  I: Integer;
begin
  for I := List.Items.Count - 1 downto 0 do
    if List.Selected[I] then
    begin
      Items.AddObject(List.Items[I], List.Items.Objects[I]);
      List.Items.Delete(I);
    end;
end;

procedure TfrmFilterToDo.SetButtons;
var
  SrcEmpty, DstEmpty: Boolean;
begin
  SrcEmpty := SrcList.Items.Count = 0;
  DstEmpty := DstList.Items.Count = 0;
  IncludeBtn.Enabled := not SrcEmpty;
  IncAllBtn.Enabled := not SrcEmpty;
  ExcludeBtn.Enabled := not DstEmpty;
  ExAllBtn.Enabled := not DstEmpty;
end;

function TfrmFilterToDo.GetFirstSelection(List: TCustomListBox): Integer;
begin
  for Result := 0 to List.Items.Count - 1 do
    if List.Selected[Result] then Exit;
  Result := LB_ERR;
end;

procedure TfrmFilterToDo.SetItem(List: TListBox; Index: Integer);
var
  MaxIndex: Integer;
begin
  with List do
  begin
    MaxIndex := List.Items.Count - 1;
    if Index = LB_ERR then Index := 0
    else if Index > MaxIndex then Index := MaxIndex;
    Selected[Index] := True;
  end;
  SetButtons;
end;

procedure TfrmFilterToDo.cbName1Click(Sender: TObject);
begin
  EnableControlByTag(Self, (Sender as TWinControl), (Sender as TWinControl).Tag, (Sender as TDBCheckBoxEh).Checked);
end;

procedure TfrmFilterToDo.FormCreate(Sender: TObject);
begin
  dsStreets.Open;
  dsMakers.Open;
  dsTraits.Open;
  FillListBox;
  SetButtons;
  dteEdate.Value := Now;
  dteBdate.Value := Now;
  dteMakeEdate.Value := Now;
  dteMakeBdate.Value := Now;
end;

procedure TfrmFilterToDo.aFilterClearExecute(Sender: TObject);
begin
  ExAllBtnClick(nil);
  inherited;
end;

procedure TfrmFilterToDo.aApplyExecute(Sender: TObject);
begin
  Self.Close;
  (Self.Owner as TrstToDo).aFilterApply;
end;

procedure TfrmFilterToDo.dteBdateChange(Sender: TObject);
begin
  if VarIsNull(dteBdate.Value) or VarIsNull(dteEdate.Value) then exit;
  if VarToDateTime(dteEdate.Value) < VarToDateTime(dteBdate.Value) then
    dteEdate.Value := dteBdate.Value;
end;

procedure TfrmFilterToDo.dteMakeBdateChange(Sender: TObject);
begin
  if VarIsNull(dteMakeBdate.Value) or VarIsNull(dteMakeEdate.Value) then exit;
  if VarToDateTime(dteMakeEdate.Value) < VarToDateTime(dteMakeBdate.Value) then
    dteMakeEdate.Value := dteMakeBdate.Value;
end;

procedure TfrmFilterToDo.eStreetKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    Self.FindNextControl((Sender as TWinControl), true, true, false).SetFocus;
end;

end.
